/*
脚本说明:用户权限维护，给用户添加权限。
程序员:
*/

//脚本唯一关键字,通过该关键字找到对应脚本，每个脚本必须有该描述。
var v_script_id = 'authuserinfos_3';
//脚本状态,0:该脚本不许执行，1:该脚本可以执行
var v_status = '1';

//脚本逻辑写在run语句之间
if (run && action.getAuthority().is(5,"9")){
	var authid = action.form.getInteger('authid');
	var checkids_user = action.form.getString('checkids_users');
	var checkids_kinds = action.form.getString('checkids_kinds_user');
	action.errorCode = 0;
	//h_auth_user_infos
	var arrkinds = checkids_kinds.split(',');
	var arruser =  checkids_user.split(',');
	for(i=0; i<arrkinds.length; i++){
		for(j=0; j<arruser.length; j++){
			sql = "select count(id) from h_auth_user_infos where authid = " + 
					authid + " and authkind = " + arrkinds[i] + " and userid = " + arruser[j] ;
			if( action.dbUtil.getSampleIntegerValue(sql) == 0 ){
				sql = "insert into h_auth_user_infos(id,authid,authkind,userid) " +
					" values(h_auth_user_info_seq.nextval,"+authid+","+arrkinds[i]+","+arruser[j]+")";
				if(action.dbUtil.update("",sql)==0) 
				{
					action.errorCode = 2;//数据库操作失败
					action.retString = '数据库操作失败';//错误文字说明
				}
			}
		} 
	}
	//刷新权限分配缓存
	if (action.errorCode ==0){
		com.handy.plugin.auth.TAuthCache.getInstance().loadAuthUserInfos();
	}
		
}